#include <18F4455.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#rom int 0xf00000={1,2,3,4}
#include <usb_cdc.h>
#include <usb_bootloader.h>
#include <lcd.c>                          //This LCD will e changed to <lcd420.c>
#include <kbd.c>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=RFID)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_B7, stream=XBEE)
#include <string.h>

//Temporar Variables
int i;
int j;
char dump;

// Xbee Variables
int xbeeReturnStatus;
char data[30];
char data_num[10];

//RFID Variables
char header;
char rfiddata[20];
char frame[25];
char UID[20];


// FLAGS USED
int RFID_DATA_READY=0;
int FLAG_DISPLAY=0;
int STATE=0;



void main()
{

/* System Initializations */
usb_cdc_init();
usb_init();
lcd_init();
kbd_init();


while(1)
{

// Receive Packet Protocol : <Product Name> <Product Price>_<Total>_

if ( (kbhit(XBEE)) && STATE == 1 )
{
            i=0;
            j=0;
            dump = getc(XBEE);
     
            while (dump != '_')
                  {
                  data[i++]=dump;
                  dump = getc(XBEE);
                  }
            
            dump = getc(XBEE);
            
            while (dump != '_')
               {
               data_num[j++]=dump;
               dump = getc(XBEE);
               }
            FLAG_DISPLAY = 2;
            }
// Receive Packet Protocol : <Status><Welcome {UserName}>_  

if ( kbhit(XBEE) && (STATE == 0) )
{
            i=0;
            dump = getc(XBEE);
            
            if (STATE == 0 && dump == '1')
            { STATE = 1; }
     
            while (dump != '_')
                  {
                  
                  data[i++]=dump;
                  dump = getc(XBEE);
                  
                  }
            dump = getc(XBEE);
            FLAG_DISPLAY = 1;
            
    }


while (kbhit(RFID)){
         header = getc();
         for (i=0;i<15;i++){ 
               rfiddata[i]=getc(RFID);
              }
     
         RFID_DATA_READY = 1;
         
    }
               
if (RFID_DATA_READY == 1 && STATE == 0){

     frame[0]='A';
     
     //Saving UID
     
     for (i=0;i<10;i++)
     {
     UID[i]= rfiddata[i];
     }
     
     for (i=1;i<=12;i++)
         {
         frame[i] = rfiddata[i-1];
         }
     frame[13] = '\n';
     
     // ***** Sending Frame *******
      for (i=0;i<=13;i++)
          {
          putc(frame[i]);
          }
     
     RFID_DATA_READY = 0;
     
     }
     
if (RFID_DATA_READY == 1 && STATE == 1){

     frame[0]='B';
     
     //ProductID
     for (i=1;i<=12;i++)
         {
         frame[i] = rfiddata[i-1];
         }
     //MemberID
     for (i=13;i<23;i++)
     {
         frame[i] = UID[i-13];
     }
     
     frame[23] = '\n';
     
     // ***** Sending Frame *******
      for (i=0;i<=23;i++)
          {
          putc(frame[i]);
          }
     
     RFID_DATA_READY = 0;
     
     }

            
     
     
if ( FLAG_DISPLAY == 1){
     lcd_gotoxy(1,1);
     printf(USB_CDC_PUTC,"%s",data);
     printf(LCD_PUTC,"%s",data);
     
     memset(data,0,sizeof(data));
     FLAG_DISPLAY == 0;
     }
     
     
if ( FLAG_DISPLAY == 2){
     lcd_gotoxy(1,1);
     printf(USB_CDC_PUTC,"%s",data);
     printf(LCD_PUTC,"%s\n%s",data,data_num);
     
     memset(data,0,sizeof(data));
     memset(data,0,sizeof(data_num));
     
     FLAG_DISPLAY == 0;
     }
     
 }
}